package com.kt.uokos.yimo.wx.controller;

import com.kt.uokos.yimo.dao.Category;
import com.kt.uokos.yimo.service.CategoryService;
import com.kt.uokos.yimo.util.ResponseUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: Wx小程序总类目
 * @className: WxCatalogController.java
 * @author: Kang Hao
 * @date: 2022/4/24 15:17
 **/

@RestController
@RequestMapping("/wx/catalog/")

public class WxCatalogController {

    @Resource
    private CategoryService categoryService;


// 与index思路一致
    @GetMapping("current")
    public Object current(@NotNull Integer id) {

        Category currentCategory = categoryService.queryById(id);
        if(currentCategory == null){
            return ResponseUtil.badArgumentValue();
        }
        List<Category> currentSubCategory = categoryService.queryByPid(currentCategory.getId());

        Map<String, Object> map = new HashMap();
        map.put("currentCategory", currentCategory);
        map.put("currentSubCategory", currentSubCategory);
        return ResponseUtil.ok(map);
    }

    @GetMapping("/index")
    public Object index(Integer id){
        //先获取所有一级类目
        List<Category> categoryL1List = categoryService.queryAllL1();

        //根据前端传过来的L1类目id来查找分类目，如果为空则默认第一个
        Category currentCategory = null;
        if (id != null) {
            currentCategory = categoryService.queryById(id);
        } else {
                currentCategory = categoryL1List.get(0);
        }

        //根据1级类目查询2级类目
        List<Category> categoryL2List = null;
        if (currentCategory != null){
            categoryL2List = categoryService.queryByPid(currentCategory.getId());
        }

        Map<String, Object> map = new HashMap();
        map.put("categoryList", categoryL1List);
        map.put("currentCategory", currentCategory);
        map.put("currentSubCategory", categoryL2List);
        return ResponseUtil.ok(map);
    }


}
